package seentao.stdev.mobile.io.controller;

import javax.annotation.Resource;
import javax.servlet.ServletContext;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import seentao.stdev.mobile.base.model.DataModel;
import seentao.stdev.mobile.base.model.Model;
import seentao.stdev.mobile.io.service.UrlFolderService;

@RestController
@RequestMapping("/serv/folder")
public class UrlFolderController {
	
	@Resource
	private ServletContext servletContext;
	
	@Resource
	private UrlFolderService urlFolderService;
	
	@RequestMapping("list")
	public Model list(String baseUrl) {
		String basePath = servletContext.getRealPath(baseUrl);
		urlFolderService.setBasePath(basePath);
		urlFolderService.setBaseUrl(toBaseUrl(baseUrl));
		return new DataModel(urlFolderService.list());
	}
	
	private String toBaseUrl(String url) {
		String ctx = servletContext.getContextPath();
		if (url.startsWith(ctx)) {
			return url;
		} else {
			return ctx + url;
		}
	}

}
